﻿using System.Linq;
using NetBay.Core.BusinessEntities;
using NetBay.Core.BusinessEntities.Versioning;

namespace Rio.Framework.Pages.Extensions
{
    public static class DocumentEx
    {

        /// <summary>
        /// Return the current document version
        /// </summary>
        /// <param name="p_oSource"></param>
        /// <returns></returns>
        public static VersioningItem GetCurrentVersion(this Document p_oSource)
        {
            if (p_oSource.AllVersion != null)
            {
                foreach (var item in p_oSource.AllVersion)
                {
                    if (item.IsCurrentVersion)
                        return item;
                }
            }
            return null;
        }

        /// <summary>
        /// Gets the last version item.
        /// </summary>
        /// <param name="document">The document.</param>
        /// <returns></returns>
        public static VersioningItem GetLastVersionItem(this Document document)
        {
            if (document != null)
            {
                if (document.AllVersion != null && document.AllVersion.Count > 0)
                {
                    return document.AllVersion.First();
                }
            }
            return null;
        }

        /// <summary>
        /// Return the number of major version in the document
        /// </summary>
        /// <param name="p_oSource"></param>
        /// <returns></returns>
        public static int CurrentMajorCount(this Document p_oSource)
        {
            if (p_oSource.AllVersion != null && p_oSource.AllVersion.Count > 0)
            {
                return p_oSource.AllVersion.Count(i => i.Minor == 0);
            }
            return 0;
        }

        /// <summary>
        ///  Return the number of minor version in the document
        /// </summary>
        /// <param name="p_oSource"></param>
        /// <returns></returns>
        public static int CurrentMinorCount(this Document p_oSource)
        {
            if (p_oSource.AllVersion != null && p_oSource.AllVersion.Count > 0)
            {
                int lastMajor = p_oSource.AllVersion.First().Major;
                return p_oSource.AllVersion.Where(i => i.Major == lastMajor && i.Minor > 0).Count();
            }
            return 0;
        }

        /// <summary>
        /// Indicate whether the max major version is reached
        /// </summary>
        /// <param name="p_oSource"></param>
        /// <returns></returns>
        public static bool MaxMajorReached(this Document p_oSource)
        {
            //if (p_oSource.CurrentModelVersion.MaxMajorVersion <= p_oSource.CurrentMajorCount())
            //    return true;
            //else
            return false;
        }

        /// <summary>
        /// Indicate whether the max minor version is reached
        /// </summary>
        /// <param name="p_oSource"></param>
        /// <returns></returns>
        public static bool MaxMinorReached(this Document p_oSource)
        {
            //if (p_oSource.CurrentModelVersion.MaxMinorVersion <= p_oSource.CurrentMinorCount())
            //    return true;
            //else
            return false;
        }


    }
}
